package com.liuguang.testassert.controller;

import com.alibaba.fastjson.JSONObject;
import com.liuguang.testassert.common.Result;
import com.liuguang.testassert.enums.ResultFlag;
import com.liuguang.testassert.pojo.User;
import com.liuguang.testassert.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: SecurityController
 * @Description：
 * @Author: wanghr
 * Date: 11:05 AM 2020/6/16
 * Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/sc")
public class SecurityController {

    @Autowired
    private UserService userService;

    //注册用户
    @RequestMapping(value = "/userEnroll")
    public Result<String> userEnroll(String username, String password) {
        Result<String> result = new Result<>();
        log.info("入参：{}，{}", username, password);
        result = userService.userEnroll(username, password);
        return result;
    }

    //从其他系统登录进来，ROLE_admin
    @Secured("ROLE_admin")
    @RequestMapping("/fromOtherSys1")
    public Result<String> fromOtherSys1() {
        Result result = new Result("0", "操作成功");
        result.setData("从其他系统登录进来，角色是 ROLE_admin");
        return result;
    }

    //从其他系统登录进来，ROLE_student
    @Secured("ROLE_student")
//    @PreAuthorize("student")
    @RequestMapping("/fromOtherSys2")
    public Result<String> fromOtherSys2() {
        Result result = new Result("0", "操作成功");
        result.setData("从其他系统登录进来,角色是 ROLE_student");
        return result;
    }
    //从其他系统登录进来，ROLE_teacher
    @Secured("ROLE_teacher")
    @RequestMapping("/fromOtherSys3")
    public Result<String> fromOtherSys3() {
        Result result = new Result("0", "操作成功");
        result.setData("从其他系统登录进来,角色是 ROLE_teacher");
        return result;
    }

    //查询所有用户
    @RequestMapping("/selectAllUser")
    public Result<List<User>> selectAllUser() {
        Result<List<User>> result = new Result<>();
        result = userService.selectAllUser();
        return result;
    }
}
